package com.fayuan.nocv.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fayuan.nocv.entity.HealthClock;
import com.fayuan.nocv.service.HealthClockService;
import com.fayuan.nocv.vo.DataView;
import com.fayuan.nocv.vo.HealthClockVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Controller
@RequestMapping("/healthClockManage")
public class HealthClockController {


    @Autowired
    private HealthClockService healthClockService;

    @GetMapping("/healthClockPage")
    @ResponseBody
    public DataView pageHealthClock(HealthClockVo healthClockVo){

        IPage<HealthClock>page = new Page<>(healthClockVo.getPage(),healthClockVo.getLimit());

        QueryWrapper<HealthClock>wrapper = new QueryWrapper<>();
        if (healthClockVo.getUsername()!=null){ wrapper.like("username",healthClockVo.getUsername());}
        if (healthClockVo.getPhone()!=null){ wrapper.like("phone",healthClockVo.getPhone());}

        IPage<HealthClock> page1 = healthClockService.page(page, wrapper);
        DataView dataView = new DataView(page1.getTotal(),page1.getRecords());

        return dataView;
    }

    @PostMapping("/saveOrUpdate")
    @ResponseBody
    public DataView save(HealthClock healthClock){
        healthClockService.saveOrUpdate(healthClock);
        DataView dataView = new DataView();
        dataView.setMsg(healthClock.getUsername()+"添加成功");
        dataView.setCode(200);
         return dataView;
    }

    @PostMapping("/deleteById")
    @ResponseBody
    public DataView delete(Integer id){
        HealthClock byId = healthClockService.getById(id);
        String username = byId.getUsername();

        healthClockService.removeById(id);
        DataView dataView = new DataView();
        dataView.setCode(200);
        dataView.setMsg(username+"删除成功");

        return dataView;
    }

}
